package nettypro.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.commons.lang.CharSet;

import java.nio.charset.Charset;
import java.util.UUID;

/**
 * @ClassName MyServerHandler
 * @Description TODO
 * @Author zeny
 * @Date 2020/3/26 0026 13:46
 */
public class MyServerHandler extends SimpleChannelInboundHandler<MessageProtocol> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageProtocol msg) throws Exception {
        System.out.println("服务器收到的消息: " + new String(msg.getContent(), Charset.forName("utf-8")));
        System.out.println("服务器收到的消息长度: " + msg.getLen());

        //回送
        MessageProtocol messageProtocol = new MessageProtocol();
        messageProtocol.setContent("hello, 客户端".getBytes(Charset.forName("utf-8")));
        messageProtocol.setLen("hello, 客户端".getBytes().length);
        ctx.writeAndFlush(messageProtocol);
    }
}
